/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.sungrid.seine.oss.framework.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

public class EncodingFilter implements Filter {
	private String uriFilter; 	// URI列表(以英文逗号分隔)
	
	protected boolean enable;
	protected FilterConfig filterConfig;
	protected String targetEncoding;

	public EncodingFilter() {
		this.targetEncoding = "GBK";
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		
		loadConfigParams();
	}

	private void loadConfigParams() {
		this.targetEncoding = this.filterConfig.getInitParameter("encoding");
		this.uriFilter = this.filterConfig.getInitParameter("exclude");
		
		String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
		if (strIgnoreFlag.equalsIgnoreCase("true")) {
			this.enable = true;
		} else {
			this.enable = false;
		}
	}

	public void destroy() {
		this.filterConfig = null;
		this.targetEncoding = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// 判断是否需要GBK编码
		if (invokeModule((HttpServletRequest)request, (HttpServletResponse)response)) {
			chain.doFilter(request, response);
			return;
		}
		
		if (this.enable) {
			request.setCharacterEncoding(this.targetEncoding);
		}

		response.setContentType("text/html; charset=" + this.targetEncoding);
		if (!(request instanceof HttpServletRequest)) {
			chain.doFilter(request, response);
			return;
		}

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		if (httpRequest.isRequestedSessionIdFromURL()) {
			HttpSession session = httpRequest.getSession();
			if (session != null) {
				session.invalidate();
			}

		}

		HttpServletResponseWrapper wrappedResponse = new HttpServletResponseWrapper(httpResponse) {
			public String encodeRedirectUrl(String url) {
				return url;
			}

			public String encodeRedirectURL(String url) {
				return url;
			}

			public String encodeUrl(String url) {
				return url;
			}

			public String encodeURL(String url) {
				return url;
			}
		};
		chain.doFilter(request, wrappedResponse);
	}
	
	/**
	 * 是否需要GBK编码
	 * 
	 * @param request
	 * @param response
	 * 
	 * @return true:不需要验证,	false:需要验证
	 */
	private boolean invokeModule(HttpServletRequest request, HttpServletResponse response) {
		if (uriFilter != null && !"".equals(uriFilter.trim())) { // 符合URL
			String uri = request.getRequestURI();
			String[] urls = uriFilter.split(",");
			for(String url:urls)
				if(url.equals(uri))
					return true;
		}
		
		return false;
	}
}